﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using PipeTest.Game;

namespace PipeTest.Script
{
    public class ScriptManager
    {
        private ScriptExecutor scriptExecutor;

        public ScriptExecutor ScriptExecutor
        {
            get { return scriptExecutor; }
        }

        private List<ScriptAction> ParseScript(string script)
        {
            var scriptActions=new List<ScriptAction>();

            if (!string.IsNullOrEmpty(script))
            {
                var stringReader = new StringReader(script);
                string line = null;
                string[] element = null;
                var lineSplitor = new char[] {' '};
                do
                {
                    if (line != null)
                    {
                        var action = new ScriptAction();
                        var dotIndex = line.IndexOf(':');
                        if (dotIndex > -1)
                        {
                            action.Name = line.Substring(0, dotIndex);
                            element = line.Substring(dotIndex+1).Split(lineSplitor, StringSplitOptions.RemoveEmptyEntries);
                            action.ActionType = JudgeActionType(element[0]);
                            if (element.Length > 1)
                            {
                                action.Parameter = element[1];
                            }
                        }
                        else
                        {
                            element = line.Split(lineSplitor, StringSplitOptions.RemoveEmptyEntries);
                            action.ActionType = JudgeActionType(element[0]);
                            if (element.Length > 1)
                            {
                                action.Parameter = element[1];
                            }
                        }
                        scriptActions.Add(action);
                    }
                    line = stringReader.ReadLine();
                } while (!string.IsNullOrEmpty(line));
            }
            return scriptActions;
        }

        private ActionType JudgeActionType(string actionType)
        {
            if(!string.IsNullOrEmpty(actionType))
            {
                switch (actionType.ToLower())
                {
                    case "wait":
                        return ActionType.Wait;
                    case "buw":
                        return ActionType.BringupWindow;
                    case "sendkey":
                        return ActionType.SendKey;
                    case "goto":
                        return ActionType.GoTo;
                    default:
                        return ActionType.UnKnown;
                }
                
            }else
            {
                return ActionType.UnKnown;
            }
        }

        public void ExecuteScript(string script, GameVistor gameVistor)
        {
            var scriptContext=new ScriptContext
                                  {
                                      ActionsToexecute = ParseScript(script),
                                      GameVistor = gameVistor,
                                      CurrentActionIndex = 0
                                  };
            scriptExecutor = new ScriptExecutor(scriptContext);
            ThreadPool.QueueUserWorkItem(scriptExecutor.Execute);
        }

        public void Stop()
        {
            if (scriptExecutor != null)
                scriptExecutor.Stop();
        }
    }
}
